import { defineStore } from 'pinia'
import QuotaServe from '@/api/quota'
import type { QuotaStoreState } from '../type'

interface IIndustryListParams {
  division: string | undefined
  id: string | number | undefined
}

interface IQuotaListParams {
  type: string
}

interface IQuotaDetailParams {
  typeId: string | number
}
interface IQuotaPriceParams {
  isCollect: string | number
}

const useQuotaStore = defineStore('quota', {
  state: (): QuotaStoreState => {
    return {
      industryList: [],
      quotaList: [],
      favList: [],
      quotaDetail: null,
      priceList: []
    }
  },

  getters: {
    favHomeList: state => {
      const priceData = state.priceList
      priceData.forEach(ele => {
        ele.industryName = '国家重点管控资源'
        ele.division = '生产总值'
        ele.industryKey = 'JJTJ_' + ele.id
        ele.quotaName = ele.name
        ele.quotaId = ele.id
      })
      const allArr = [...state.favList, ...priceData]
      return allArr.splice(0, 2)
    },
    favTree: state => {
      console.log('[favTree] ---- calc start ----')

      const priceData = state.priceList
      priceData.forEach(ele => {
        ele.industryName = '国家重点管控资源'
        ele.division = '生产总值'
        ele.industryKey = 'JJTJ_' + ele.id
        ele.quotaName = ele.name
        ele.quotaId = ele.id
        ele.predictEntity = {
          prediction: ele.prediction,
          real: ele.real
        }
      })
      const favAllList = [...state.favList, ...priceData]
      const industryNameList = [
        ...new Set<string>(favAllList.map(item => item.industryKey.split('_')[0]))
      ]
      const ft = industryNameList.reduce((mm: any, division) => {
        const curList = favAllList.filter(item => item.industryKey.split('_')[0] == division)

        //   const industryTypeSet = new Set<string>(curList.map(item => item.industryName))
        //   console.log('indeustryTypeSet', industryTypeSet)

        //   const qt = [...industryTypeSet].reduce((m: any, industryName: string) => {
        //     const industryList = curList.filter(q => q.industryName == industryName)
        //     const secondModuleList = [...new Set(industryList.map(i => i.quotaName))]
        //     const secondModules: any = []
        //     secondModuleList.forEach(s => {
        //       const favList = curList.filter(
        //         q => q.industryName == industryName && q.secondModuleName == s
        //       )
        //       secondModules.push({
        //         name: s,
        //         children: favList
        //       })
        //     })
        //     m.push({ name: industryName, children: secondModules })

        //     return m
        //   }, [])
        mm.push({
          name: division,
          list: curList
        })
        return mm
      }, [])
      return ft
    },
    quotaTree: state => {
      const industryTypeSet = new Set<string>(state.quotaList.map(item => item.industryType))
      let seq = 0 // submodule sequence

      const qt = [...industryTypeSet].reduce((m: any, industryType: string) => {
        const industryList = state.quotaList.filter(q => q.industryType == industryType)
        const secondModuleList = [...new Set(industryList.map(i => i.secondModuleName))]
        const secondModules: any = []
        secondModuleList.forEach(s => {
          const quotaList = state.quotaList.filter(
            q => q.industryType == industryType && q.secondModuleName == s
          )
          quotaList.forEach(q => {
            q.seq = seq
          })
          seq++

          secondModules.push({
            name: s,
            children: quotaList
          })
        })
        m.push({ name: industryType, children: secondModules })

        return m
      }, [])

      return qt
    }
  },

  actions: {
    /**
     * 行业列表
     * @param params IIndustryListParams
     */
    async fetchIndustryList(params: IIndustryListParams) {
      try {
        const resp = await QuotaServe.getIndustryList(params)
        this.industryList = resp.data
      } catch (error) {}
    },

    /**
     * 行业订阅指标列表
     * @param params IQuotaListParams
     */
    async fetchQuotaList(params: IQuotaListParams) {
      try {
        const resp = await QuotaServe.getQuotaList(params)
        console.log(resp, 33333333)
        this.quotaList = resp.data
      } catch (error) {
        console.log(error)
      }
    },
    /**
     * 行业订阅指标列表
     * @param params IQuotaListParams
     */
    async fetchFavQuotaList() {
      try {
        const resp = await QuotaServe.getQuotaFavList()
        this.favList = resp.data
      } catch (error) {
        console.log(error)
      }
    },

    async fetchQuotaDetail(params: IQuotaDetailParams) {
      try {
        const resp = await QuotaServe.getQuotaDetail(params)
        this.quotaDetail = resp.data
      } catch (error) {}
    },

    async fetchPricePrediction(params: IQuotaPriceParams) {
      try {
        const resp = await QuotaServe.getPricePrediction(params)
        this.priceList = resp.data
      } catch (error) {
        console.log(error)
      }
    }
  }
})

export { useQuotaStore }
